<?php
class Tag_model extends CI_Model {
	
	public function get_tag_by_id($tag_id)
	{
		$sql = 'SELECT * FROM tag WHERE tagId=? LIMIT 1';
		$query = $this->db->query($sql, array($tag_id));

		return $query->num_rows > 0 ? $query->row() : false;
	}
	
	public function get_tag_by_name($user_id, $tag)
	{
		$sql = 'SELECT * FROM tag WHERE userId=? AND tagName=? LIMIT 1';
		$query = $this->db->query($sql, array($user_id, $tag));
		
		return $query->num_rows > 0 ? $query->row() : false;
	}
	
	public function get_tag_list($user_id)
	{
		$sql = 'SELECT * FROM tag WHERE userId=?';
		$query = $this->db->query($sql, array($user_id));
		
		return $query->result();
	}
	
	public function add_tag($user_id, $tag)
	{
		$sql = 'INSERT INTO tag (userId, tagName, tagCount) VALUES (?, ?, 0)';
		$query = $this->db->query($sql, array($user_id, $tag));

		return $this->db->affected_rows() > 0 ? $this->db->insert_id() : false;
	}
	
	public function update_tag($tag_id, $tag)
	{
		$sql = 'UPDATE tag SET tagName=? WHERE tagId=? LIMIT 1';	
		$query = $this->db->query($sql, array($tag, $tag_id));
		
		return $this->db->affected_rows() > 0;
	}
	
	public function del_tag($tag_id)
	{
		$sql = "DELETE FROM tag WHERE tagId=? LIMIT 1";
		$query = $this->db->query($sql, array($tag_id));

		return $this->db->affected_rows() > 0;
	}
	
	public function plus_count($user_id, $tag, $count = 1)
	{
		$sql = 'UPDATE tag SET tagCount=tagCount+? WHERE userId=? AND tagName=? LIMIT 1';	
		$query = $this->db->query($sql, array($count, $user_id, $tag));
		
		return $this->db->affected_rows() > 0;
	}
	
	public function minus_count($user_id, $tag, $count = 1)
	{
		$sql = 'UPDATE tag SET tagCount=tagCount-? WHERE userId=? AND tagName=? LIMIT 1';
		$query = $this->db->query($sql, array($count, $user_id, $tag));
		
		return $this->db->affected_rows() > 0;
	}
}
?>